﻿MyView = require './my_view'
DataSources = require 'dataaccess/data_sources'
#CRUD for a single model
module.exports = class MyFormView extends MyView
	
	getSaveButtonName: => null
	getDeleteButtonName: => null

	initialize: ->
		super

	render: ->
		console.debug 'WineDetailsView#render', this, @$el
		$(@containerSelector).html(@$el)
		super

	saveModel:=>
		console.debug @id+'#saveModel-Start'
		@updateModelFromView()
				
		if !@model.isNew()
			@model.save()
		else
			src= DataSources.GetWines();
			@model.url = src.url;
			@model.save()
				.done (response) =>
					src.create(response);
				.fail (response) =>
					@model.dispose()
		console.debug @id+'#saveModel-End'

	deleteModel: => 
		console.debug @id+'#deleteWine-Start'
		@model.destroy();
		console.debug @id+'#deleteWine-End'


	#implement to update the model with the values from the view
	updateModelFromView: ->null

	subscribeToControlsEvents: =>
		@delegate 'click', @getSaveButtonName(), @saveModel
		@delegate 'click', @getDeleteButtonName(), @deleteModel

	subscribeToDataSourceEvents: =>
		@modelBind 'destroy', @remove
